package cn.remexs.examples.apijson.parser;

import apijson.JSONResponse;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.RequestRole;
import apijson.StringUtil;
import apijson.framework.APIJSONFunctionParser;
import apijson.orm.JSONRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import javax.servlet.http.HttpSession;

/**可远程调用的函数类
 * @author remexs
 */
public class CustomFunctionParser extends APIJSONFunctionParser {

  public static final String TAG = "CustomFunctionParser";

  public CustomFunctionParser() {
    this(null, null, 0, null, null);
  }

  public CustomFunctionParser(
    RequestMethod method,
    String tag,
    int version,
    JSONObject request,
    HttpSession session
  ) {
    super(method, tag, version, request, session);
  }

  /**
   * @param current
   * @param idList
   * @return
   * @throws Exception
   */
  public Object verifyIdList(
    @NotNull JSONObject current,
    @NotNull String idList
  )
    throws Exception {
    Object obj = current.get(idList);
    if (obj == null) {
      return null;
    }

    if (obj instanceof Collection == false) {
      throw new IllegalArgumentException(
        idList + " 不符合 Array 类型! 结构必须是 [] ！"
      );
    }
    JSONArray array = (JSONArray) obj;
    if (array != null) {
      for (int i = 0; i < array.size(); i++) {
        if (
          array.get(i) instanceof Long == false &&
          array.get(i) instanceof Integer == false
        ) {
          throw new IllegalArgumentException(
            idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!"
          );
        }
      }
    }
    return null;
  }

  /**
   * @param current
   * @param urlList
   * @return
   * @throws Exception
   */
  public Object verifyURLList(
    @NotNull JSONObject current,
    @NotNull String urlList
  )
    throws Exception {
    Object obj = current.get(urlList);
    if (obj == null) {
      return null;
    }

    if (obj instanceof Collection == false) {
      throw new IllegalArgumentException(
        urlList + " 不符合 Array 类型! 结构必须是 [] ！"
      );
    }
    JSONArray array = (JSONArray) obj;
    if (array != null) {
      for (int i = 0; i < array.size(); i++) {
        if (StringUtil.isUrl(array.getString(i)) == false) {
          throw new IllegalArgumentException(
            urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!"
          );
        }
      }
    }
    return null;
  }

  /**
   * @param current
   * @param momentId
   * @return
   * @throws Exception
   */
  public int deleteCommentOfMoment(
    @NotNull JSONObject current,
    @NotNull String momentId
  )
    throws Exception {
    long mid = current.getLongValue(momentId);
    if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
      return 0;
    }

    JSONRequest request = new JSONRequest();

    //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    JSONRequest comment = new JSONRequest();
    comment.put("momentId", mid);

    request.put("Comment", comment);
    //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    JSONObject rp = new CustomRequestParser(RequestMethod.DELETE)
      .setNeedVerify(false)
      .parseResponse(request);

    JSONObject c = rp.getJSONObject("Comment");
    return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT);
  }

  /**删除评论的子评论
   * @param current
   * @param toId
   * @return
   */
  public int deleteChildComment(
    @NotNull JSONObject current,
    @NotNull String toId
  )
    throws Exception {
    long tid = current.getLongValue(toId);
    if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
      return 0;
    }

    //递归获取到全部子评论id

    JSONRequest request = new JSONRequest();

    //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    JSONRequest comment = new JSONRequest();
    comment.put("id{}", getChildCommentIdList(tid));

    request.put("Comment", comment);
    //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    JSONObject rp = new CustomRequestParser(RequestMethod.DELETE)
      .setNeedVerify(false)
      .parseResponse(request);

    JSONObject c = rp.getJSONObject("Comment");
    return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT);
  }

  private JSONArray getChildCommentIdList(long tid) {
    JSONArray arr = new JSONArray();

    JSONRequest request = new JSONRequest();

    //Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    JSONRequest idItem = new JSONRequest();

    //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    JSONRequest comment = new JSONRequest();
    comment.put("toId", tid);
    comment.setColumn("id");
    idItem.put("Comment", comment);
    //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    request.putAll(idItem.toArray(0, 0, "Comment-id"));
    //Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    JSONObject rp = new CustomRequestParser()
      .setNeedVerify(false)
      .parseResponse(request);

    JSONArray a = rp.getJSONArray("Comment-id[]");
    if (a != null) {
      arr.addAll(a);

      JSONArray a2;
      for (int i = 0; i < a.size(); i++) {
        a2 = getChildCommentIdList(a.getLongValue(i));
        if (a2 != null) {
          arr.addAll(a2);
        }
      }
    }

    return arr;
  }

  /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
   * @param current
   * @return JSONArray 只能用JSONArray，用long[]会在SQLConfig解析崩溃
   * @throws Exception
   */
  public JSONArray getIdList(@NotNull JSONObject current) {
    return new JSONArray(
      new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710))
    );
  }

  /**TODO 仅用来测试 "key-()":"verifyAccess()"
   * @param current
   * @return
   * @throws Exception
   */
  public Object verifyAccess(@NotNull JSONObject current) throws Exception {
    long userId = current.getLongValue(JSONRequest.KEY_USER_ID);
    RequestRole role = RequestRole.get(current.getString(JSONRequest.KEY_ROLE));
    if (
      role == RequestRole.OWNER &&
      userId != CustomVerifier.getVisitorId(getSession())
    ) {
      throw new IllegalAccessException("登录用户与角色OWNER不匹配！");
    }
    return null;
  }
}
